package com.example.taobaounion.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.example.taobaounion.R
import com.example.taobaounion.ui.fragment.HomeFragment
import com.example.taobaounion.ui.fragment.RedPackageFragment
import com.example.taobaounion.ui.fragment.SearchFragment
import com.example.taobaounion.ui.fragment.SelectFragment
import com.example.taobaounion.utils.LogUtil
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {

    lateinit var mTransaction: FragmentTransaction
    val mFragmentList: ArrayList<Fragment> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        mFragmentList.add(HomeFragment())
        mFragmentList.add(RedPackageFragment())
        mFragmentList.add(SelectFragment())
        mFragmentList.add(SearchFragment())
        mTransaction = supportFragmentManager.beginTransaction()
        mTransaction.add(R.id.container, mFragmentList[0],"home")
        mTransaction.add(R.id.container, mFragmentList[1],"redPackage").hide( mFragmentList[1])
        mTransaction.add(R.id.container, mFragmentList[2],"select").hide( mFragmentList[2])
        mTransaction.add(R.id.container, mFragmentList[3],"search").hide( mFragmentList[3])
        mTransaction.commit()
        bottomNavigation.setOnNavigationItemSelectedListener { it ->
            mTransaction = supportFragmentManager.beginTransaction()
            when (it.itemId) {
                R.id.menu_home -> {

                    mFragmentList.forEach {

                        if (it.tag=="home"){
                            mTransaction.show(it)
                        }else{
                            mTransaction.hide(it)
                        }
                    }
                }
                R.id.menu_red_package -> {
                   // mTransaction.replace(R.id.container, mFragmentList[1])
                    mFragmentList.forEach {
                        if (it.tag=="redPackage"){
                            mTransaction.show(it)
                        }else {
                            mTransaction.hide(it)
                        }
                    }
                }
                R.id.menu_select ->{
                   // mTransaction.replace(R.id.container, mFragmentList[2])
                    mFragmentList.forEach {
                        if (it.tag=="select"){
                            mTransaction.show(it)
                        }else{
                            mTransaction.hide(it)
                        }
                    }
                }
                R.id.menu_search -> {
                   // mTransaction.replace(R.id.container, mFragmentList[3])
                    mFragmentList.forEach {
                        if (it.tag=="search"){
                            mTransaction.show(it)
                        }else{
                            mTransaction.hide(it)
                        }
                    }
                }

            }
            mTransaction.commit()
            true
        }
    }
}